<template>
  <div class="page-person">
    <!-- 头部区域 -->
    <div class="main-top">
      <div class="avator">
        <el-upload ref="upload"
                   class="avatar-uploader"
                   action="http://192.168.43.78:8081/upload"
                   :show-file-list="false"
                   :on-success="uploadImage">
          <div @mouseenter="avatarActive=true"
               @mouseleave="avatarActive=false">
            <!-- 头像 -->
            <img :src="userinfo.avatar"
                 alt="">
            <transition enter-active-class="animate__animated  animate__fadeIn"
                        :duration="{enter:200}">
              <div class="mask"
                   v-show="avatarActive">修改头像</div>
            </transition>
          </div>

        </el-upload>
      </div>
      <!-- 用户相关信息 -->
      <div class="base-mes">
        <h2>{{userinfo.nickname}} <span>
            <img v-if="userinfo.sex==1"
                 :src="sexMan"
                 alt="">
            <img v-else
                 :src="sexWomen"
                 alt=""></span></h2>
        <p class="sign">{{userinfo.sign}}</p>
      </div>
    </div>
    <!-- 个人中心导航 -->
    <ul class="person-nav">
      <li v-for="item in personNav"
          :key="item.path">
        <router-link :to="item.path"
                     :class="{'is-active':$route.meta.num ==item.num}">{{item.text}}</router-link>
      </li>
    </ul>
    <div class="main-cont">
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
import defaultavator from '@/assets/images/default-avator.png'
import sexMan from '@/assets/svg/sex-man.svg'
import sexWomen from '@/assets/svg/sex-women.svg'
import { mapState, mapActions } from 'vuex'
import { updateInfo, personInfo } from '@/api/user' // 用户接口
export default {
  data() {
    return {
      defaultavator, // 默认头像
      sexMan, // 男性图片
      sexWomen, // 女性图片
      avatarActive: false, // 提示修改
      ruleForm: {},
      personNav: [
        { path: '/person/article', text: '文章', num: 1 },
        { path: '/person/collection', text: '收藏', num: 2 },
        { path: '/person/orders', text: '订单', num: 3 },
        { path: '/person/settings', text: '编辑', num: 4 }
      ]
    }
  },
  methods: {
    ...mapActions(['getUserInfo']),
    // 上传图像
    uploadImage(res) {
      this.ruleForm.avatar = res.data  // 最新图像
      updateInfo(this.ruleForm).then(res => {
        this.getUserInfo() // 获取最新数据修改vuex中userinfo
      })
    },
    async initUserInfo() {
      let { data: res } = await personInfo()
      this.ruleForm = res.data
    }
  },
  computed: {
    ...mapState(['userinfo'])
  },
  created() {
    this.initUserInfo()
  }
}
</script>

<style lang="less" scoped>
.page-person {
  margin: 30px auto 50px auto;
  width: 900px;
  /* 用户信息 */
  .main-top {
    display: flex;
    padding-bottom: 10px;
    border-bottom: 1px solid #ccc;
    /* 头像 */
    .avatar-uploader {
      position: relative;
      height: 80px;
      width: 80px;
      border-radius: 50%;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        cursor: pointer;
      }
      .mask {
        padding: 3px 0;
        position: absolute;
        top: 0;
        left: 0;
        height: 80px;
        width: 100%;
        text-align: center;
        line-height: 80px;
        font-size: 13px;
        color: #fff;
        background: rgba(0, 0, 0, 0.4);
      }
    }
    /* 基本信息 */
    .base-mes {
      width: 750px;
      margin-left: 20px;
      h2 {
        margin: 7px 0 10px 0;
        display: flex;
        align-items: center;
        height: 30px;
        font-size: 20px;
        letter-spacing: 1px;
        img {
          margin-left: 2px;
          width: 25px;
          height: 25px;
        }
      }
      .sign {
        font-size: 14px;
      }
    }
  }
  /* 导航 */
  .person-nav {
    display: flex;
    .is-active {
      color: #33c5c5;
    }
    a {
      display: block;
      padding: 13px 20px;
      font-family: '黑体';
      letter-spacing: 1px;
      color: #969696;
      &:hover {
        color: #33c5c5;
      }
    }
  }
  .main-cont {
    width: 95%;
  }
}
</style>